
import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.ftplet.Authority;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.listener.ListenerFactory;
import org.apache.ftpserver.usermanager.impl.BaseUser;
import org.apache.ftpserver.usermanager.impl.WritePermission;

import java.net.UnknownHostException;
import java.util.Arrays;

public class Ftpd {
	public static void ftp(int port, String homeDirectory) {
		var serverFactory = new FtpServerFactory();
		var factory = new ListenerFactory();
		factory.setPort(port);
		serverFactory.addListener("default", factory.createListener());

		var user = new BaseUser();
		user.setName("anonymous");
		user.setHomeDirectory(homeDirectory);
		user.setAuthorities(Arrays.asList((Authority) new WritePermission()));
		var server = serverFactory.createServer();
		try {
			serverFactory.getUserManager().save(user);
			server.start();
		} catch (FtpException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws UnknownHostException {
		ftp(21, "/srv/media/www");
	}
}
